import router from "@/router/index";

import store from '@/store/index'

// 导航守卫（前置）在路由跳转之前拦截
router.beforeEach((to, from, next)=>{
    console.log(to.path)
    console.log(from)
    
    if(to.meta.title){
        document.title = to.meta.title
    }else{
        document.title = "搭建系统"
    }

    if (store.state.user.token == ""){
        console.log("未登录")
        if (to.path == '/login'){
            next()
            console.log("去登录页面")
        } else{
            console.log("去其他页面")
            next('/login')
        }
    }else{
        console.log("登录")
        if (to.path == '/login'){
            console.log(to.path)
            console.log(from.path)
            next(from.path)
            console.log("去登录页面")
        } else{
            console.log("去其他页面")
            next()
        }
    }
})

router.afterEach((to) => {
    if (to.path != '/login') {
        if (JSON.stringify(store.state.tagsView.tagsView).indexOf(to.path)==-1){
            store.commit("addTagsView", {title:to.meta.title,path:to.path,name:to.name})
        }
    }
})
